package com.dyb.demo.initclass;

/**
 * @author dyb
 * @Description 初始化及类加载测试
 * @date 2019/2/14 10:31
 */
public class TestClassInit {

}

/*
 程序加载先试图访问Beetle.main(), 编译器加载class文件时发现Beetle有基类，则加载Insect，将Insect中的static初始化，
 然后初始化导出类Beetle中的static。接着继续执行Beetle.main()，new Beetle()时访问构造方法，默认调用super(), 加载
 Insect类的构造方法，完成后继续执行Beetle类的构造方法，调用k时对他进行初始化。
 */

/**
 * 昆虫
 * @author dyb
 */
class Insect {
    private int i = 9;
    protected int j;
    /**
     * 默认为包修饰符，在不同包下不能调用new Insect();
     * @author dyb
     */
    Insect () {
        System.out.println("i = " + i + ", j = " + j);
        j = 39;
    }
    private static int x1 = printInt("static Insect.x1 initialized");
    static int printInt(String s) {
        System.out.println(s);
        return 47;
    }
}

/**
 * 甲虫
 * @author dyb
 */
class Beetle extends Insect {
    private int k = printInt("Beetle.k initialized");
    public Beetle() {
        // 默认执行 super()
        System.out.println("k = " + k);
        // super.j == j
        System.out.println("j = " + j);
    }

    private static  int x2 = printInt("static Beetle.x2 initialized");

    public static void main(String[] args) {
        System.out.println("Beetle Constructor");
        Beetle beetle = new Beetle();
    }
}

